/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBillBrowseDialog.java
 *
 * Created on Apr 6, 2011, 10:08:25 AM
 */
package inventorymanagement;

import DataAccess.TableAccess;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import model.ColorTable;

/**
 *
 * @author Truongbv_b00929
 */
public class frmBillBrowseDialog extends javax.swing.JDialog {

    static frmProcedureBill code;

    /** Creates new form frmBillBrowseDialog */
    public frmBillBrowseDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(((int) screenSize.getWidth() - this.getWidth()) / 2,
                ((int) screenSize.getHeight() - this.getHeight()) / 2);
        refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtSearchBillID = new javax.swing.JTextField();
        btnSearchRefresh = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBills = new ColorTable();
        btnSelect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Bill Browse");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(0, 153, 255), null));

        jLabel1.setText("Bill ID:");

        txtSearchBillID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchBillIDKeyReleased(evt);
            }
        });

        btnSearchRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Refresh.png"))); // NOI18N
        btnSearchRefresh.setText("Refresh");
        btnSearchRefresh.setPreferredSize(new java.awt.Dimension(100, 23));
        btnSearchRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchRefreshActionPerformed(evt);
            }
        });

        tblBills.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tblBills.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tblBills);

        btnSelect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Check.png"))); // NOI18N
        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(8, 8, 8)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtSearchBillID, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSearchRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnSelect)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearchBillID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSelect)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSearchBillIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchBillIDKeyReleased
        String str = txtSearchBillID.getText();
        String sql = "";
        if (interact.iCheckForm.checkNumber(str)) {
            JOptionPane.showMessageDialog(this, "Invalid fomat", "Error", JOptionPane.ERROR_MESSAGE);
            txtSearchBillID.setText("");
            return;
        }
        if (str.equals("")) {
            sql = "select * from Bills where BillID not in(select BillID from Receipt)";
        } else {
            sql = "select * from Bills where BillID not in(select BillID from Receipt) And BillID like '%" + str + "%'";
        }
        //        if (!isNumberic(str)) {
        //            JOptionPane.showMessageDialog(this, "Invalid fomat", "Error", JOptionPane.ERROR_MESSAGE);
        //            refresh();
        //            return;
        //        } else {
        //            sql = ("select BillID, CustomerID, Date, Status from Bills where BillID=" + Integer.valueOf(str));
        //        }
        try {
            TableAccess.readToTableBill(sql, tblBills);
        } catch (Exception e) {
            e.printStackTrace();
        }
}//GEN-LAST:event_txtSearchBillIDKeyReleased

    private void btnSearchRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchRefreshActionPerformed
        refresh();
}//GEN-LAST:event_btnSearchRefreshActionPerformed

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        int billID = tblBills.getSelectedRow();
        if (billID == -1) {
            JOptionPane.showMessageDialog(this, "You must choose a item");
            return;
        } else {
            int row = tblBills.getSelectedRow();
            code.txtBillID.setText(tblBills.getValueAt(row, 0).toString());
            this.dispose();
        }
}//GEN-LAST:event_btnSelectActionPerformed

    private void refresh() {
        String sql = ("select * from Bills where BillID not in(select BillID from Receipt)");
        txtSearchBillID.setText("");
        try {
            TableAccess.readToTableBill(sql, tblBills);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                frmBillBrowseDialog dialog = new frmBillBrowseDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearchRefresh;
    private javax.swing.JButton btnSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblBills;
    private javax.swing.JTextField txtSearchBillID;
    // End of variables declaration//GEN-END:variables
}
